export function answerQueries(nums: number[], queries: number[]): number[] {
    const n: number = nums.length, m: number = queries.length;
    // ts 排序
    nums.sort((a, b) => a - b);
    // const 大小不变？
    const f: number[] = new Array(n+1).fill(0);
    for (let i=0; i<n; ++i) {
        f[i+1] = f[i] + nums[i];
    }
    const ans: number[] = new Array(m).fill(0);
    for (let i=0; i<m; ++i) {
        ans[i] = binarySearch(f, queries[i]) - 1;
    }
    return ans;
}

function binarySearch(f: number[], target: number) {
    // 用const报错，常量？
    let left: number = 1;
    let right: number = f.length;
    while (left < right) {
        // let mid: number = (left + right) / 2;
        // 要加Math.floor！！
        let mid: number = Math.floor((left + right) / 2);
        if (f[mid] > target) {
            right = mid;
        } else {
            left = mid + 1;
        }
    }
    return left;
}